package com.aaa.sbm.service.impl;

import com.aaa.sbm.dao.DeptDao;
import com.aaa.sbm.entity.Dept;
import com.aaa.sbm.service.DeptService;
import com.aaa.sbm.util.CustomException;
import com.aaa.sbm.util.Page;
import com.aaa.sbm.util.ResultStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;

/**
 * @FileName: DeptServiceImpl
 * @Description:
 * @Author: zhz
 * @CreateTime: 2024/2/27 11:14
 * @Version: 1.0.0
 */
@Service
@Log4j2
public class DeptServiceImpl  implements DeptService {

    public DeptServiceImpl() {
        log.info("-----------------------DeptServiceImpl被构建-----------");
    }

    //依赖注入
    @Resource
    private DeptDao deptDao;

    @Override
    public int add(Dept dept) {
        return deptDao.add(dept);
    }

    @Override
    public int update(Dept dept) {
        return deptDao.update(dept);
    }

    @Override
    public int deleteById(int deptNo) {
        return deptDao.deleteById(deptNo);
    }

    @Override
    public PageInfo queryByPage(Page<Dept> page){
        //获取参数
        Integer pageNo = page.getPageNo();
        Integer pageSize = page.getPageSize();
        //判断 没传，或者传递0 都是错误
        if(pageNo==null||pageNo==0||pageSize==null||pageSize==0){
            //抛出自定义异常
            throw  new CustomException(ResultStatus.PARAM_NOT_EMPTY.getReturnCode(),
                    ResultStatus.PARAM_NOT_EMPTY.getReturnMessage());
        }
        //设值分页参数   startPage(int pageNum, int pageSize)  pageNum 第几页  pageSize 页面
        PageHelper.startPage(pageNo,pageSize);
        return new PageInfo(deptDao.queryByParam(page.getData()));
    }
    @Override
    @Async
    public Dept queryById(int deptNo) {
        final Dept[] dept = {null};
        //this.queryByIdA(deptNo);

        log.info("我是本来查询方法=====================");

        CompletableFuture.runAsync(()->{
            try {
                Thread.sleep(10000);
                 dept[0] =deptDao.queryById(deptNo);
            }catch (Exception e){

            }
        });
        return  null;
    }

    @Override
    //@Async
    public Dept queryByIdA(int deptNo) {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("我是异步调用方法---------------------------");
        return null;
    }
}
